# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import traceback

class BookPipeline(object):
    def open_spider(self, spider):
        self.count = 0
        self.file = open('jd_book.txt', 'a', encoding='utf-8')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        try:
            self.count += 1
            print("count: " + str(self.count))
            self.file.write(json.dumps(item,ensure_ascii=False,indent=2))
            self.file.write("\n")
        except:
            print(traceback.print_exc())
            pass
        # print(item)
        # return item
